/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
/**
 * \file
 * \brief Protocol dispatcher for serching HUB messages
 * \author Antti Maula <antti.maula@tkk.fi>
 */
#ifndef _PROTOCOLDISPATCHER_H_
#define _PROTOCOLDISPATCHER_H_

#ifdef __cplusplus
extern "C" {
#endif

#include "connection.h"
#include "connectionTCP.h"
#include "connectionUNIX.h"
#include "protocolID.h"
#include "buffer.h"


#define PROTOCOLDISPATCHER_OK             0
#define PROTOCOLDISPATCHER_ERROR          -1

#define PROTOCOLDISPATCHER_HUBPORT_UNIX   -1

typedef void (*protocolDispatcherPtr) (int protocol_number,
				       const unsigned char *data,
				       int size,
                                       void *userPtr);

typedef struct _protocolDisp {
  TConnection link; ///< Link 
  protocolDispatcherPtr protocolDispatcherTable[MAX_PROTOCOL_COUNT]; ///< Protocol handler table
  void *protocolDispatcherUserPtrTable[MAX_PROTOCOL_COUNT];
  TBuffer data_in;
} TProtocolDisp;

int protocolDispInit(TProtocolDisp *pd);
void protocolDispDeinit(TProtocolDisp *pd);
int protocolDispConnect(TProtocolDisp *pd, 
			const char *hubaddr,
			int hubport);
int protocolDispDisconnect(TProtocolDisp *pd);
int protocolDispRegisterCallback(TProtocolDisp *pd, 
				 unsigned char protocol_number,
				 protocolDispatcherPtr p,
                                 void *userPtr);
int protocolDispProcess(TProtocolDisp *pd, int timeout);

int protocolDispSendPacket(TProtocolDisp *pd, const unsigned char *data, int dlen);
int protocolDispSendPacketEx(TProtocolDisp *pd, 
			     const unsigned char *header, 
			     int headerlen,
			     const unsigned char *payload,
			     int payloadlen);

#ifdef __cplusplus
}
#endif

#endif //_PROTOCOLDISPATCHER_H_
